import { Component, OnInit } from '@angular/core';
import { HttpClient  } from '@angular/common/http';
import { Router } from '@angular/router';

@Component({
  selector: 'app-navbar',
  templateUrl: './navbar.component.html',
  styleUrls: ['./navbar.component.css']
})
export class NavbarComponent implements OnInit {
  user = JSON.parse(window.localStorage.getItem('userinfo') || '{}')

  constructor(
    private router: Router,
    private http: HttpClient,

  ) { }

  ngOnInit(): void {
  }

  signout(){
    // console.log("go")
    //去除默认请求事件
    // e.preventDefault()
    this.http.delete('http://localhost:3000/session')
    .toPromise()
    .then(data =>{
      
      window.localStorage.removeItem('auth_token')
      window.localStorage.removeItem('userinfo')
      this.router.navigate(['/signin'])
    }).catch(err => {
      window.alert("退出失败，请稍后重试\n"+err.message)
    })
  }

}
